import 'package:flutter/cupertino.dart';

///item 动画
class AnimationItem extends AnimatedBuilder {
  //动画控制器
  final AnimationController? animationController;

  //列表总数
  final int? count;

  //列表当前数
  final int? index;

  //动画的widget
  final Widget? childWidget;

  //是否是水平移动 默认不是
  final bool? isHorizontal;

  AnimationItem(
      {Key? key,
      required this.animationController,
      required this.count,
      required this.index,
      required this.childWidget,
      this.isHorizontal = false})
      : super(
            key: key,
            animation: animationController!,
            builder: (BuildContext context, Widget? child) {
              //初始化动画值
              Animation<double>? animation =
                  Tween<double>(begin: 0.0, end: 1.0).animate(
                CurvedAnimation(
                  parent: animationController,
                  curve: Interval((1 / count!) * index!, 1.0,
                      curve: Curves.fastOutSlowIn),
                ),
              );
              return FadeTransition(
                opacity: animation,
                child: Transform(
                  transform: isHorizontal!
                      ? Matrix4.translationValues(
                          100 * (1.0 - animation.value), 0.0, 0.0)
                      : Matrix4.translationValues(
                          0.0, 50 * (1.0 - animation.value), 0.0),
                  child: childWidget,
                ),
              );
            });
}
